﻿using Tutorial1_Spawner;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Random = Unity.Mathematics.Random;

namespace Tutorial4_Singleton_Aspect
{
    public readonly partial struct CubeRotateAndMoveAspect : IAspect
    {
        private readonly RefRW<LocalTransform> _localTransform;
        private readonly RefRO<CubeTag> _tag;

        public void RotateAndMove(ref Random random, double elapsedTime, float deltaTime)
        {
            var moveSpeed = random.NextFloat(1, 50);
            _localTransform.ValueRW.Position.y = (float)math.sin(moveSpeed * elapsedTime);

            var rotateSpeed = random.NextFloat(1, 10);
            // 不知道为什么必须重新赋值回去才行，否则旋转不生效
            _localTransform.ValueRW = _localTransform.ValueRW.RotateY(rotateSpeed * deltaTime);
            // 更新随机种子，下次就不一样了
            random.state = random.NextUInt();
        }
    }
}